﻿using RayDen.RayEngine.Core;
using RayDen.RayEngine.Core.Devices;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Threads;
using RayDen.RayEngine.Managers;
using RayDen.RayEngine.Samplers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RayDen.RayEngine.Engines.RayTracer
{
    [RenderingEngine(EngineType = RenderingEngines.RayTracer)]
    public class WhittedRayTracerEngine : BaseRenderingEngine
    {
        public WhittedRayTracerEngine(RayEngineScene scene) : base(scene)
        {
            //ImageFilmBase.SampleBufferSize
        }

        public override RenderThread CreateRenderThread(int threadIndex, IntersectionEngine intersectionDevice, RayEngineScene scene, IImageFilm film, IFrameManager frameManager, SamplingContext samplingContext)
        {
            return new DeviceRenderThread<WrtPathBuffer> (threadIndex, intersectionDevice, scene, LowLatencySampling, film, samplingContext,frameManager);
        }

        public override ISampler CreateSampler(int width, int height, int maxPathDepth, int threadIndex, int screenStartLine, int windowHeight)
        {
            return SamplerFactory.Instance.CreateSampler(width, height, maxPathDepth, screenStartLine, LowLatencySampling,
                                                  threadIndex, windowHeight);
           // var sampler = new UniformSampler { lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine > 0 ? screenStartLine + 1 : screenStartLine, screenWidth = width, screenHeight = height };
            var sampler = new RandomSampler
                {
                    lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine > 0 ? screenStartLine + 1 : screenStartLine, screenWidth = width, screenHeight = height
                };

            //var sampler = new UniformTileSampler { lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine > 0 ? screenStartLine + 1: screenStartLine, screenWidth = width, screenHeight = height };
            //var sampler = new UniformSampler() { lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine, screenWidth = width, screenHeight = height};
            //var sampler = new RandomSampler() { lowLatency = LowLatencySampling, tIdx = threadIndex, screenStartLine = screenStartLine, screenWidth = width, screenHeight = height };
            //var sampler = new LDSampler() { lowLatency = LowLatencySampling, sceneMaxPathDepth = maxPathDepth, screenWidth = width, screenHeight = height};

            return sampler;
        }

        public override IImageFilm CreateFilm(int width, int height) {

            return new ImageFilm<RayTracerPathSampler>(width, height);
        }
    }
}
